;
define(["MComponent"], function(MComponent) {
	/**
	 * 图像组件
	 * @param {Object} background 背景
	 */
	var MImage = function(background) {
		this.dom = this.query("<div><img/></div>","MImage");
		var img = this.dom.children("img");
		this.setBackground(background);
		
		//图像数据
		var dataSrc = null;
		
		//默认图像数据
		var defaultSrc = null;
		
		var width = 0;
		
		var height = 0;
		
		/**
		 * 设置数据图像
		 */
		this.setSrc = function(src){
			dataSrc = src;
			return this;
		};
		
		/**
		 * 获得数据图像
		 */
		this.getSrc = function(){
			return dataSrc;
		}
		
		/**
		 * 获得默认加载的数据图像
		 */
		this.setDefaultSrc = function(src){
			defaultSrc = src;
			img.attr("src",defaultSrc);
		}
		
		/**
		 * 加载图像
		 */
		this.loadImage = function(){
			img.attr("src",dataSrc);
		};
		
		/**
		 * 添加样式
		 */
		this.addStyle = function(key,val){
			this.setStyle(key, val);
			this.setStyle("-ms-"+key, val);
			this.setStyle("-moz-"+key, val);
			this.setStyle("-webkit-"+key, val);
			this.setStyle("-o-"+key,val );
		}
		
		/**
		 * 反转颜色
		 */
		this.invert = function(val){
			if(!val){
				val = 100;
			}
			this.addStyle("filter","invert("+val+"%)");
			
			var ieVal = val / 100;
			
			var that = this;
			var ie6 = function(){
				that.setStyle("filter","progid:DXImageTransform.Microsoft.BasicImage(invert="+ieVal+")");
			};
			
			mofum.ieCallback(ie6,ie6,ie6,null,ie6);
		};
		
		/**
		 * 水平翻转
		 */
		this.flipx = function(){
			this.addStyle("transform","scaleX(-1)");
			
			var that = this;
			var ie6 = function(){
				var filter = that.getStyle("filter").replace("FlipV","");
				if(filter.indexOf("FlipH") != -1){
					that.setStyle("filter",filter);
				}else{
					that.setStyle("filter","FlipH " + filter);
				}
				
			};
			
			mofum.ieCallback(ie6,ie6,ie6,null,ie6);
		};
		
		/**
		 * 高斯模糊
		 */
		this.blur = function(value){
			var val = mofum.replacePx(value);
			this.addStyle("filter","blur("+val+"px)");
			var that = this;
			var ie6 = function(){
				that.setStyle("filter","progid:DXImageTransform.Microsoft.Blur(PixelRadius='"+val+"')");
			};
			mofum.ieCallback(ie6,ie6,ie6,null,ie6);
		}
		
		/**
		 * 垂直翻转
		 */
		this.flipy = function(){
			this.addStyle("transform","scaleY(-1)");
			
			var that = this;
			var ie6 = function(){
				var filter = that.getStyle("filter").replace("FlipH","");
				if(filter.indexOf("FlipV") != -1){
					that.setStyle("filter",filter);
				}else{
					that.setStyle("filter","FlipV " + filter);
				}
			};
			mofum.ieCallback(ie6,ie6,ie6,null,ie6);
		};
		
		/**
		 * 灰度
		 */
		this.grayscale = function(val){
			if(!val){
				val = 100;
			}
			this.addStyle("filter","grayscale("+val+"%)");
			
			var ieVal = val / 100;
			
			var that = this;
			var ie6 = function(){
				that.setStyle("filter","progid:DXImageTransform.Microsoft.BasicImage(grayScale="+ieVal+")");
			};
			
			mofum.ieCallback(ie6,ie6,ie6,null,ie6);
		};
		
		/**
		 * 正常
		 */
		this.normal = function(val){
			this.addStyle("filter","");
			this.addStyle("transform","");
		};
		
		
		/**
		 * 设置高度
		 */
		this.setHeight = function(value) {
			var val = parseInt(value);
			this.setStyle("height", val + "px");
			img.css("height", val + "px");
			return this;
		};

		/**
		 * 设置宽度
		 */
		this.setWidth = function(value) {
			var val = parseInt(value);
			this.setStyle("width", val + "px");
			img.css("width", val + "px");
			return this;
		};
		
		this.init();
	};
	mofum.extend(MImage, MComponent);
	return MImage;
});